package com.hetang.maijiayun.adaptertools.adapter;

import com.hetang.maijiayun.adaptertools.b2cinterface.youzan.client.DefaultYouzanClient;
import com.hetang.maijiayun.adaptertools.b2cinterface.youzan.domain.Trade;
import com.hetang.maijiayun.adaptertools.b2cinterface.youzan.request.TradesSoldGetRequest;
import com.hetang.maijiayun.adaptertools.b2cinterface.youzan.response.TradesSoldGetResponse;
import com.hetang.maijiayun.adaptertools.controller.vo.request.DetectorRequest;
import com.hetang.maijiayun.adaptertools.utils.JsonTools;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.http.impl.cookie.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by wanda on 11/4/15.
 */
public class YouzanAdapter extends AdapterAbstract {
    /**
     * 日志
     */
    private static final Logger logger = LoggerFactory.getLogger(YouzanAdapter.class);

    @Override
    public Integer request(Map<String, String> authorizesMap, DetectorRequest request) throws Exception {
        return null;
    }

    @Override
    public List<String> requestOrders(Map<String, String> authorizesMap, DetectorRequest request) throws Exception {
        if (MapUtils.isEmpty(authorizesMap) || !authorizesMap.containsKey("url") || !authorizesMap.containsKey("appKey") || !authorizesMap.containsKey("secret") || !authorizesMap.containsKey("sessionKey")) {
            logger.error("该有赞平台店铺没有授权信息或授权信息错误！！");
            return null;
        }
        DefaultYouzanClient client = new DefaultYouzanClient(authorizesMap.get("appKey"), authorizesMap.get("secret"),authorizesMap.get("sessionKey"));
        TradesSoldGetRequest req=new TradesSoldGetRequest();
        req.setUse_has_next("true");
        req.setFields("tid");
        req.setStart_update(DateUtils.formatDate(request.getStartTime(), "yyyy-MM-dd HH:mm:ss"));
        req.setEnd_update(DateUtils.formatDate(request.getEndTime(), "yyyy-MM-dd HH:mm:ss"));
        req.setPage_no("1");
        req.setPage_size("50");
        logger.info("youzan get order interface, request time is : {}, request param is : {}", System.currentTimeMillis(), JsonTools.createJsonString(req));
        TradesSoldGetResponse rsp = client.execute(req);
        logger.info("youzan get order interface, response time is : {}, response : {}", System.currentTimeMillis(), JsonTools.createJsonString(rsp));
        if (rsp == null || rsp.getError_response() != null) {
            return null;
        }
        Long page = 1L;
//        Long mod = rsp.getTotal_results()%50L;
//        page = rsp.getTotal_results()/50L;
//        if (0 != mod) {
//            page += 1;
//        }
        List<String> result = new ArrayList<String>();
        for (Trade trade : rsp.getTrades()) {
            result.add(trade.getTid() + "");
        }
        while (rsp.getHas_next()) {
            page += 1;
            req.setPage_no(page.toString());
            rsp = client.execute(req);
            if (rsp.getError_response() != null) {
                logger.error("youzan get order interface error, response time is : {}, response : {}", System.currentTimeMillis(), JsonTools.createJsonString(rsp));
                continue;
            }
            if (CollectionUtils.isEmpty(rsp.getTrades())) {
                continue;
            }
            for (Trade trade : rsp.getTrades()) {
                result.add(trade.getTid() + "");
            }
        }
        return result;
    }
}
